package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AdminKeywordCreateBo;
import com.cskaoyan.bean.bo.AdminKeywordDeleteBo;
import com.cskaoyan.bean.bo.AdminKeywordListBo;
import com.cskaoyan.bean.bo.AdminKeywordUpdateBo;
import com.cskaoyan.bean.po.MarketKeyword;
import com.cskaoyan.bean.po.MarketKeywordExample;
import com.cskaoyan.bean.vo.AdminOrderShipVo;
import com.cskaoyan.bean.vo.admin_keyword_create.AdminKeywordCreateDataEntityVo;
import com.cskaoyan.bean.vo.admin_keyword_create.AdminKeywordCreateVo;
import com.cskaoyan.bean.vo.admin_keyword_list.AdminKeywordListDataEntityVo;
import com.cskaoyan.bean.vo.admin_keyword_list.AdminKeywordListListEntityVo;
import com.cskaoyan.bean.vo.admin_keyword_list.AdminKeywordListVo;
import com.cskaoyan.bean.vo.admin_keyword_update.AdminKeywordUpdateDataEntityVo;
import com.cskaoyan.bean.vo.admin_keyword_update.AdminKeywordUpdateVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Trellis
 * @since 2022/09/07 20:21
 */
@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {


    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public AdminKeywordListVo selectByListBo(AdminKeywordListBo keywordListBo) {
        PageHelper.startPage(keywordListBo.getPage(),keywordListBo.getLimit());
        MarketKeywordExample example = new MarketKeywordExample();
        String clause = keywordListBo.getSort()+" "+keywordListBo.getOrder();
        example.setOrderByClause(clause);
        if (keywordListBo.getKeyword() != null) {
            example.createCriteria().andKeywordLike("%"+keywordListBo.getKeyword()+"%");
        }

        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        List<AdminKeywordListListEntityVo> listEntityVos = new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {
            AdminKeywordListListEntityVo listEntityVo = new AdminKeywordListListEntityVo(marketKeyword.getIsDefault(), marketKeyword.getDeleted(), marketKeyword.getAddTime(), marketKeyword.getSortOrder(),
                    marketKeyword.getUpdateTime(), marketKeyword.getId(), marketKeyword.getKeyword(), marketKeyword.getUrl(), marketKeyword.getIsHot());
            listEntityVos.add(listEntityVo);
        }

        PageInfo pageInfo = new PageInfo(marketKeywords);
        int total = (int)pageInfo.getTotal();
        AdminKeywordListDataEntityVo dataEntityVo = new AdminKeywordListDataEntityVo(total, pageInfo.getPages(), keywordListBo.getLimit(), keywordListBo.getPage(), listEntityVos);
        AdminKeywordListVo keywordListVo = new AdminKeywordListVo(0, dataEntityVo, "成功");
        return keywordListVo;
    }

    @Override
    public AdminKeywordCreateVo insertByCreateBo(AdminKeywordCreateBo createBo) throws ParseException {
        //获取现在时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String format = dateFormat.format(date);
        //获取数据库中类型的时间
        Date time = dateFormat.parse(format);

        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setAddTime(time);
        marketKeyword.setUpdateTime(time);
        //临时数据
        marketKeyword.setSortOrder(0);
        marketKeyword.setDeleted(false);

        marketKeyword.setIsDefault(createBo.getIsDefault());
        marketKeyword.setIsHot(createBo.getIsHot());
        marketKeyword.setKeyword(createBo.getKeyword());
        marketKeyword.setUrl(createBo.getUrl());

        marketKeywordMapper.insert(marketKeyword);
        AdminKeywordCreateDataEntityVo dataEntityVo = new AdminKeywordCreateDataEntityVo(marketKeyword.getIsDefault(),marketKeyword.getAddTime(),
                marketKeyword.getUpdateTime(),marketKeyword.getId(),marketKeyword.getKeyword(),marketKeyword.getUrl(),
                marketKeyword.getIsHot());
        AdminKeywordCreateVo createVo = new AdminKeywordCreateVo(0,dataEntityVo,"成功");
        return createVo;
    }

    @Override
    public AdminKeywordUpdateVo updateByBo(AdminKeywordUpdateBo updateBo) {
        MarketKeyword marketKeyword = new MarketKeyword();
        //未给数据 临时数据
        marketKeyword.setSortOrder(0);
        marketKeyword.setIsDefault(updateBo.isDefault());
        marketKeyword.setAddTime(updateBo.getAddTime());
        marketKeyword.setUpdateTime(updateBo.getUpdateTime());
        marketKeyword.setId(updateBo.getId());
        marketKeyword.setKeyword(updateBo.getKeyword());
        marketKeyword.setUrl(updateBo.getUrl());
        marketKeyword.setIsHot(updateBo.isHot());
        //临时数据
        marketKeyword.setDeleted(false);

        int keySelective = marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        AdminKeywordUpdateDataEntityVo entityVo = null;
        if (keySelective == 1) {
            entityVo = new AdminKeywordUpdateDataEntityVo(updateBo.isDefault(), updateBo.getAddTime(),
                    updateBo.getUpdateTime(), updateBo.getId(), updateBo.getKeyword(), updateBo.getUrl(), updateBo.isHot());
        }
        AdminKeywordUpdateVo updateVo = new AdminKeywordUpdateVo(0, entityVo, "成功");
        return updateVo;
    }

    @Override
    public AdminOrderShipVo deleteByBo(AdminKeywordDeleteBo deleteBo) {
        int id = deleteBo.getId();
        int key = marketKeywordMapper.deleteByPrimaryKey(id);
        AdminOrderShipVo orderShipVo = new AdminOrderShipVo();
        if (key == 1) {
            orderShipVo.setErrno(0);
            orderShipVo.setErrmsg("成功");
        }
        return orderShipVo;
    }
}
